package cn.kmia.hrm.service;

import cn.kmia.hrm.pojo.PostInfo;
import cn.kmia.hrm.util.CommonSet;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * @program: hrm
 * @description: 岗位信息服务
 * @author: 4K
 * @create: 2018-07-19 16:20
 **/
@IocBean
public class PostInfoService extends BaseService {

    private static final Log log = Logs.get();

    /**
     * 分页查询
     *
     * @param name
     * @param floor
     * @param pager
     * @return
     */
    public QueryResult queryByPage(String name, String verify_status, int floor, Pager pager) {
        Cnd cnd = Cnd.where("1", "=", 1);
        if (!Strings.isBlank(name) && !"null".equals(name)) {
            cnd.and("name", "like", "%" + name + "%");
        }
        if (!Strings.isBlank(verify_status) && !"null".equals(verify_status)) {
            cnd.and("verify_status", "=", verify_status);
        }
        if (floor != 0) {
            cnd.and("floor", "=", floor);
        }
        cnd.and("delFlag", "is", null);
        List<PostInfo> list = dao.query(PostInfo.class, cnd, pager);
        QueryResult qr = new QueryResult();
        qr.setList(list);
        pager.setRecordCount(dao.count(PostInfo.class, cnd));
        qr.setPager(pager);
        return qr;
    }

    /**
     * 查询
     *
     * @param floor
     * @return
     */
    public List<PostInfo> query(int floor) {
        return dao.query(PostInfo.class, Cnd.where("delFlag", "is", null).and("floor", "=", floor).and("verify_status", "=", CommonSet.Verify_Pass));
    }

    /**
     * 管理查询
     *
     * @param floor
     * @return
     */
    public List<PostInfo> queryManage(int floor) {
        return dao.query(PostInfo.class, Cnd.where("delFlag", "is", null).and("floor", "=", floor));
    }

    /**
     * 新增
     *
     * @param postInfo    对象信息
     * @param user_idcard 操作人
     * @return
     */
    public PostInfo add(PostInfo postInfo, String user_idcard) {

        postInfo.setVerify_status(CommonSet.Verify_Not);// 新增状态为未审核
        postInfo.setOpBy(user_idcard);
        postInfo.setCreateTime(new Timestamp(new Date().getTime()));
        postInfo.setUpdateTime(new Timestamp(new Date().getTime()));
        postInfo = dao.insert(postInfo);
        return postInfo;
    }

    /**
     * 更新
     *
     * @param postInfo    对象信息
     * @param user_idcard 操作人
     * @return
     */
    public boolean update(PostInfo postInfo, String user_idcard) {
        postInfo.setOpBy(user_idcard);
        postInfo.setUpdateTime(new Timestamp(new Date().getTime()));
        return dao.updateIgnoreNull(postInfo) > 0 ? true : false;
    }

    /**
     * 删除，实际标记delFlag为true
     *
     * @param ids
     * @return
     */
    public NutMap del(int[] ids) {
        NutMap nm = new NutMap();
        if (ids != null && ids.length > 0) {
            dao.update(PostInfo.class, Chain.make("delFlag", true), Cnd.where("id", "in", ids));
            nm.setv("success", true);
        } else {
            nm.setv("success", false).setv("errorMsg", "请重试！");
        }
        return nm;
    }

    /**
     * 查询名称是否存在
     *
     * @param postInfoName
     * @return 存在 true,不存在 false
     */
    public boolean checkPostInfo(String postInfoName) {
        int count = dao.count(PostInfo.class, Cnd.where("name", "=", postInfoName));
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取对象
     *
     * @param id
     * @return
     */
    public PostInfo getPostInfo(int id) {
        return dao.fetch(PostInfo.class, Cnd.where("id", "=", id));
    }

    /**
     * 审核
     * @param ids
     * @param verify_status
     * @param user_idcard
     * @return
     */
    public NutMap verify(int[] ids,String verify_status, String user_idcard) {
        NutMap nm = new NutMap();
        if (ids != null && ids.length > 0) {
            dao.update(PostInfo.class, Chain.make("verify_status", verify_status), Cnd.where("id", "in", ids));
            dao.update(PostInfo.class, Chain.make("opBy", user_idcard), Cnd.where("id", "in", ids));
            dao.update(PostInfo.class, Chain.make("updateTime", new Date()), Cnd.where("id", "in", ids));
            nm.setv("success", true);
        } else {
            nm.setv("success", false).setv("errorMsg", "请重试！");
        }
        return nm;
    }
}
